//
//  MineTableViewController.m
//  CarHome
//
//  Created by lanou3g on 15-4-25.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MineTableViewController.h"
#import "CollectViewController.h"
#import "ContrastTableViewController.h"
#import "HomeTableViewController.h"


#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)


@interface MineTableViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property (nonatomic, retain) NSArray *titleArr1;
@property (nonatomic, retain) NSArray *titleArr2;
@property (nonatomic, retain) NSArray *titleArr3;

@property (nonatomic,retain) NSMutableDictionary *dataDic;
@property (nonatomic,retain) NSMutableArray *allKeys;

@property (nonatomic,retain) UISwitch *nightSwitch;
@property (nonatomic, retain) NSDictionary *colorDict;//用于存放切换背景颜色
//@property (nonatomic) BOOL onis;//判断模式状态

@end

@implementation MineTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    
    
    [self reloadData];
    
}

- (void)reloadData
{
    self.titleArr1 = @[@"我的收藏", @"车型比较"];
    self.titleArr2 = @[@"清除缓存", @"夜间模式"];
    self.titleArr3 = @[@"关于汽车之家"];
    
    //放入数组字典中
    self.dataDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:_titleArr1, @"arr1", _titleArr2, @"arr2", _titleArr3, @"arr3", nil];
    //获取所有key值
    NSArray *keys = [_dataDic allKeys];
    //将不可变数组转化为可变数组
    self.allKeys = [NSMutableArray arrayWithArray:keys];
    
    [_allKeys sortUsingSelector:@selector(compare:)];
    
    
}

#pragma mark - 布局tebleView
- (void)layoutTableView
{
    //代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //行高
    self.tableView.rowHeight = 40;
}



#pragma mark - Table view data source
#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [_allKeys objectAtIndex:section];//在key中保存了section
    NSMutableArray *group = [_dataDic valueForKey:key];
    return group.count;
    
}

#pragma mark - cell显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifer = @"identifer";
    NSString *key = [_allKeys objectAtIndex:indexPath.section];
    NSMutableArray *group = [_dataDic valueForKey:key];
    NSString *str = [group objectAtIndex:indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifer];
        }
    
    cell.textLabel.text = str;
    
    if (indexPath.section == 2 || indexPath.section == 0) {
        cell.detailTextLabel.text = @">";
        
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            
        }else{
            if (!_nightSwitch) {
                
                self.nightSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 55, 5, 60, 20)];
                [cell addSubview:_nightSwitch];
            }
           
          
            [_nightSwitch addTarget:self action:@selector(mySwitchChanged:) forControlEvents:UIControlEventValueChanged];
            
        }
        
    }
   
   //加图片~~~~~~~

    return cell;
}

#pragma mark - 黑夜模式
- (void)mySwitchChanged:(UISwitch *)sender
{
    if (sender.isOn == YES) {
        NSLog(@"打开了");
        
        //发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kaicolors" object:nil];//
        self.view.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.5];
    
     
        
    } else if (sender.isOn == NO) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"guancolors" object:nil];
        self.view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.9];
    }
    
    
    
    
    
    
    
}




#pragma mark - cell被点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            CollectViewController *collectTVC = [[CollectViewController alloc] init];
            [self.navigationController pushViewController:collectTVC animated:YES];
        }else{
            ContrastTableViewController *contrastTVC = [[ContrastTableViewController alloc] init];
            [self.navigationController pushViewController:contrastTVC animated:YES];
            //[self.navigationController pushViewController:contrastTVC animated:YES];
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否清除缓存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alertView show];
        }else{
            
            NSLog(@"aaaa");
        }
    }else{
        HomeTableViewController *homeTVC = [[HomeTableViewController alloc] init];
        [self.navigationController pushViewController:homeTVC animated:YES];
    }
    
   
}

#pragma mark - 分区footer
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
#pragma mark - 分区footer显示的view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
    
    return view;
    
}







@end
